package org.ezplatform.office.forum.web.rest;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ezplatform.cloud.common.log.LogService;
import org.ezplatform.core.pagination.PropertyFilter;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.office.forum.entity.ForumSensitive;
import org.ezplatform.office.forum.service.ForumSensitiveService;
import org.ezplatform.util.ValidateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 敏感词控制类
 * 2019年11月2日17:40:59
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/api/w/forum/sensitive")
public class ForumSensitiveWebApiController extends BaseController<ForumSensitive, String>{
	
	private final Logger LOGGER = LoggerFactory.getLogger(ForumSensitiveWebApiController.class);

    @Autowired
    private ForumSensitiveService forumSensitiveService;

    @Autowired
   	private LogService logService;
    
    @Override
    protected BaseService<ForumSensitive, String> getEntityService() {
        return forumSensitiveService;
    }
	    
    /**
     * 初始化数据
     *
     * @param request
     * @param model
     * @param id
     */
    @ModelAttribute
    public void prepareModel(HttpServletRequest request, Model model,
                             @RequestParam(value = "id", required = false) String id) {
        super.initPrepareModel(request, model, id);
    }
	    
    /**
     * 获取列表（翻页）数据
     *
     * @param request
     * @return json
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions({"stdForum:forumCate:03"})
    public Page<Map> findByPage(HttpServletRequest request) {
        String corpId = WebUtils.getCurrentUser().getCorpId();
        Pageable pageable = PropertyFilter.buildPageableFromHttpRequest(request);
        List<PropertyFilter> filter = PropertyFilter.buildFiltersFromHttpRequest(ForumSensitive.class, request);
        Page<Map> page = forumSensitiveService.findPage(pageable, filter, corpId, TOKEN);
        return page;
    }
	    
    /**
     * 根据id获取敏感词信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions(value = {"stdForum:forumCate:03", "stdForum:forumCate:02"}, logical = Logical.OR)
    public OperationResult editShow(@RequestParam("id") String id,HttpServletRequest request) {
    	String logStatus = request.getParameter("logStatus");
    	ForumSensitive forumSensitive = forumSensitiveService.findOne(id);
    	if(!StringUtils.equals(logStatus, "1")) {
    		logService.saveModuleLog("forum:sensitive:03", forumSensitive.getSensitiveName(), "");
        }
        return OperationResult.buildSuccessResult(forumSensitive);
    }
    
    /**
     * 保存/修改敏感词
     * @param entity
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = {"stdForum:forumCate:01", "stdForum:forumCate:02"}, logical = Logical.OR)
    public OperationResult editSave(@ModelAttribute("entity") ForumSensitive entity, HttpServletRequest request, Model model) {
	    if(!ValidateUtils.validateInt(String.valueOf(entity.getSortNo())) ){
		  return OperationResult.buildFailureResult("非法参数!");
	    }
	    if(entity.getId()==null||StringUtils.isBlank(entity.getId())) {
		  logService.saveModuleLog("forum:sensitive:01", entity.getSensitiveName(), "");
	    }else {
		  ForumSensitive oldSensitive = forumSensitiveService.findOne(entity.getId());
		  logService.saveModuleLog("forum:sensitive:02", oldSensitive.getSensitiveName(), "");
	    }
    	OperationResult  result = this.editSave(entity);
        return result;
    }
    
    /**
     * 删除（批量）数据
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions({"stdForum:forumCate:04"})
    public OperationResult delete(@RequestParam("ids") String... ids) {
    	return forumSensitiveService.logicDelete(ids);
    }
    
    /**
     * 获取列表（翻页）数据
     *
     * @param request
     * @return json
     */
    @RequestMapping(value = "/getAllType", method = RequestMethod.GET)
    @ResponseBody
    public OperationResult findAllLabel(HttpServletRequest request) {
        List list =  forumSensitiveService.findAllLabel(WebUtils.getCurrentUser().getCorpId());
        return OperationResult.buildSuccessResult(list);
    }
}